#include <iostream>
#include<iomanip>
using namespace std;


void Getday(int year, int month, int day, int num)
{
    static int monthday[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
    int n = day + num;
    while (n > monthday[month])
    {
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {
            n -= 29;
            month++;
        }
        else {
            n -= monthday[month];
            month++;
        }


        if (month == 13) {
            month = 1;
            year++;
        }

    }
    cout << year << '-';
    std::cout << std::setfill('0') << std::setw(2) << month << '-';
    std::cout << std::setfill('0') << std::setw(2) << n << endl;
}
int main() {
    int n = 0;
    scanf("%d", &n);
    while (n) {
        int year, month, day, num;
        cin >> year >> month >> day >> num;
        Getday(year, month, day, num);
        n--;
    }
}